<template>
  <view>
      <daya-clone-navbar :prop_clone_navbar="clone_navbar" :prop_is_ipx="is_ipx"/>

    <navigator
      class="return-index-btn"
      openType="redirect"
      url="/pages/index/index"
      v-if="show_index"
    >
      <image
        src="https://ymqycc.oss-cn-chengdu.aliyuncs.com/ymqzho2o/static/images/nav-icon-index.png"
        style="width: 50rpx; height: 50rpx"
      ></image>
    </navigator>
    <view class="flex-row tab-bar">
      <view class="flex-grow-1">
        <navigator
          :class="status == 1 ? 'active' : ''"
          openType="redirect"
          url="order?status=1"
        >
          <text
            :style="
              'color:' +
              (status == 1 ? color : '') +
              ';border-bottom:' +
              (status == 1 ? 2 : 0) +
              'rpx solid ' +
              color
            "
            >待核销</text
          >
        </navigator>
      </view>
      <view class="flex-grow-1">
        <navigator
          :class="status == 2 ? 'active' : ''"
          openType="redirect"
          url="order?status=2"
        >
          <text
            :style="
              'color:' +
              (status == 2 ? color : '') +
              ';border-bottom:' +
              (status == 2 ? 2 : 0) +
              'rpx solid ' +
              color
            "
            >已核销</text
          >
        </navigator>
      </view>
      <view class="flex-grow-1">
        <navigator
          :class="status == 3 ? 'active' : ''"
          openType="redirect"
          url="order?status=3"
        >
          <text
            :style="
              'color:' +
              (status == 3 ? color : '') +
              ';border-bottom:' +
              (status == 3 ? 2 : 0) +
              'rpx solid ' +
              color
            "
            >已失效</text
          >
        </navigator>
      </view>
    </view>
    <view class="order-list">
      <view class="no-data-tip zwdd" v-if="order_list.length == 0 && mygd">
        <image
          mode="aspectFit"
          src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/list.png"
        ></image>
        <view>您暂无相关订单哦~</view>
      </view>

      <view
        v-if="order_list.length>0"
        class="order-item"
        v-for="(oitem, index) in order_list"
        :key="index"
      >
        <navigator :url="'reserveinfo?oid=' + oitem.order.id">
          <view class="flex-row order-info">
            <view class="flex-grow-1">订单号:{{ oitem.order_num }}</view>
            <view class="flex-grow-0" v-if="oitem.order.type == '3'"
              >{{ System.yd_name }}订单</view
            >
          </view>
          <view
            class="flex-row zysp flex-y-center"
            @click="tzsjxq(oitem.store_id)"
          >
            <image mode="aspectFill" :src="oitem.store_logo"></image>
            <view class="flex-grow-1 flex-col">
              <view class="mdname">
                {{ oitem.store_name }}
              </view>
              <view class="mdtime">下单时间：{{ oitem.pay_time }}</view>
            </view>
            <view class="flex-grow-0">
              <image
                src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/blackjt.png"
                style="width: 40rpx; height: 40rpx"
              ></image>
            </view>
          </view>
          <view class="goods-list">
            <view class="goods-item flex-row">
              <view
                class="flex-grow-0 flex-row flex-y-center"
                style="overflow: visible"
              >
                <image
                  class="goods-pic"
                  mode="aspectFill"
                  :src="oitem.good_logo"
                ></image>
              </view>
              <view class="flex-grow-1 flex-col">
                <view class="flex-grow-1 flex-row flex-y-center">
                  <view class="flex-grow-1">
                    {{ oitem.good_name }}
                    <text style="margin-left: 10rpx" v-if="oitem.num > 1"
                      >等{{ oitem.num }}件商品</text
                    >
                  </view>
                  <view class="flex-grow-0 sfmoney"
                    >实付：￥{{ oitem.money }}</view
                  >
                </view>
              </view>
            </view>
            <view class="goods-item flex-row">
              <!-- <view
                class="flex-grow-0 flex-row flex-y-center"
                style="overflow: visible"
              >
                <image class="goods-pic" mode="aspectFill" src=""></image>
              </view> -->
              <view class="flex-grow-1 flex-col">
                <view class="flex-grow-1 flex-row flex-y-center">
                  <view class="flex-grow-1">支付方式：</view>
                  <view class="flex-grow-0" v-if="oitem.pay_type == 1"
                    >微信支付</view
                  >
                  <view class="flex-grow-0" v-if="oitem.pay_type == 2"
                    >余额支付</view
                  >
                </view>
              </view>
            </view>
            <view class="goods-item flex-row">
              <!-- <view
                class="flex-grow-0 flex-row flex-y-center"
                style="overflow: visible"
              >
                <image class="goods-pic" mode="aspectFill" src=""></image>
              </view> -->
              <view class="flex-grow-1 flex-col">
                <view class="flex-grow-1 flex-row flex-y-center">
                  <view class="flex-grow-1">联系方式：</view>
                  <view class="flex-grow-0"
                    >{{ oitem.user_name }}({{ oitem.user_tel }})</view
                  >
                </view>
              </view>
            </view>
            <view class="goods-item flex-row">
              <!-- <view
                class="flex-grow-0 flex-row flex-y-center"
                style="overflow: visible"
              >
                <image class="goods-pic" mode="aspectFill" src=""></image>
              </view> -->
              <view class="flex-grow-1 flex-col">
                <view class="flex-grow-1 flex-row flex-y-center">
                  <view class="flex-grow-1">到期时间：</view>
                  <view class="flex-grow-0">{{ oitem.dq_time }}</view>
                </view>
              </view>
            </view>
            <view class="goods-item flex-row" v-if="oitem.note != ''">
              <view
                class="flex-grow-0 flex-row flex-y-center"
                style="overflow: visible"
              >
                <image class="goods-pic" mode="aspectFill" src=""></image>
              </view>
              <view class="flex-grow-1 flex-col">
                <view class="flex-grow-1 flex-row flex-y-center">
                  <view class="flex-grow-0">留言备注：</view>
                  <view class="flex-grow-1">{{ oitem.note }}</view>
                </view>
              </view>
            </view>
          </view>
        </navigator>

        <view class="flex-row">
          <view class="flex-grow-1 flex-y-center flex-row dbbtn">
            <view class="" v-if="oitem.state == 2 && status == 1">
              <button
                @tap="hxqh"
                class="ddbtn"
                :data-id="oitem.id"
                :data-sjid="oitem.store_id"
                style="border-radius: 40rpx"
              >
                核销取货
              </button>
            </view>
            <view class="" v-if="oitem.state == 2 && status == 1">
              <button
                @tap="txsj"
                class="ddbtn"
                :data-tel="oitem.tel"
                style="border-radius: 40rpx"
              >
                联系商家
              </button>
            </view>
            <view class="" v-if="status == 3">
              <button
                @tap="orderRevoke"
                class="ddbtn ddbtn3"
                :data-id="oitem.id"
                style="border-radius: 40rpx"
              >
                删除
              </button>
            </view>
          </view>
        </view>
      </view>
      <view class="jzz" v-if="!mygd">
        <image class="Rotation" src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/jzz.png"></image>
        <view>加载中...</view>
      </view>
      <view class="jzwb" v-if="mygd">
        <view class="hx"></view>
        <view class="wz">加载完毕</view>
        <view class="hx"></view>
      </view>
    </view>
    <view
      @tap="hideFun"
      :class="'flex-row flex-y-center modal1 ' + (hide == 1 ? 'hide' : '')"
    >
      <view
        class="flex-y-center"
        style="width: 100%; height: 800rpx; padding: 125rpx"
      >
        <view
          style="
            background-color: #fff;
            width: 100%;
            height: 100%;
            border-radius: 10rpx;
            padding: 0 50rpx;
          "
        >
          <view
            class="flex-x-center"
            style="width: 100%; height: 50rpx; margin: 20rpx 0; font-size: 13pt"
            >核销二维码</view
          >
          <image
            :src="'data:image/png;base64,' + hx_code"
            style="width: 400rpx; height: 400rpx"
          ></image>
          <view
            class="flex-x-center"
            style="width: 100%; height: 50rpx; font-size: 9pt"
            >出示给商家扫码核销</view
          >
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index'
import gettel from '@/ymq_canyino2o/dist/gettel/index'
var app = getApp()
var util = require('../../utils/util.js')
export default {
  components: {
    cardlist,
    gettel
  },
  data() {
    return {
      color: '#34aaff',
      status: 1,
      order_list: [],
      show_no_data_tip: false,
      hide: 1,
      qrcode: '',
      pagenum: 1,
      storelist: [],
      mygd: false,
      jzgd: true,

      System: {
        yd_name: ''
      },

      hx_code: '',


      show_index: 0,

      oitem: {
        order: {
          id: '',
          type: ''
        },

        order_num: '',
        store_logo: '',
        store_name: '',
        pay_time: '',
        good_logo: '',
        num: 0,
        money: '',
        pay_type: 0,
        user_name: '',
        user_tel: '',
        dq_time: '',
        note: '',
        state: 0,
        id: '',
        store_id: '',
        tel: ''
      }
    }
  },
  onLoad: function (t) {
    uni.setNavigationBarTitle({
      title: '我的抢购'
    })
    app.globalData.setNavigationBarColor(this)
    var that = this
    app.globalData.util.request({
      url: 'entry/wxapp/System',
      cachetime: '0',
      success: function (t) {
        console.log(t)
        var e = t.data
        if ('2' == e.model) {
          getApp().globalData.sjid = e.default_store
        }
        if ('4' == e.model) {
          getApp().globalData.sjid = e.default_store
        }
        if ('' == t.data.dc_name) {
          t.data.dc_name = '店内'
        }
        if ('' == t.data.wm_name) {
          t.data.wm_name = '外卖'
        }
        if ('' == t.data.yd_name) {
          t.data.yd_name = '预定'
        }
        that.setData({
          System: t.data
        })
      }
    })
    console.log(t)
    this.setData({
      status: t.status
    })
    this.reLoad()
  },
  onReachBottom: function () {
    console.log('上拉加载', this.pagenum)
    if (!this.mygd && this.jzgd) {
      this.setData({
        jzgd: false
      })
      this.reLoad()
    }
  },
  methods: {
    reLoad: function () {
      var t
      var that = this
      var e = this.status || 1
      var a = uni.getStorageSync('users').id
      var s = this.pagenum
      if (1 == e) {
        t = '2'
      }
      if (2 == e) {
        t = '3'
      }
      if (3 == e) {
        t = '4'
      }
      console.log(t, a, s)
      app.globalData.util.request({
        url: 'entry/wxapp/MyQgOrder',
        cachetime: '0',
        data: {
          state: t,
          type: 4 == t ? 1 : '',
          user_id: a,
          page: s,
          pagesize: 10
        },
        success: function (t) {
          console.log('分页返回的列表数据', t.data)
          for (var e = 0; e < t.data.length; e++) {
            t.data[e].dq_time = util.ormatDate(t.data[e].dq_time)
          }
          if (t.data.length < 10) {
            that.setData({
              mygd: true,
              jzgd: true
            })
          } else {
            that.setData({
              jzgd: true,
              pagenum: that.pagenum + 1
            })
          }
          var a = that.storelist
          a = (function (t) {
            for (var e = [], a = 0; a < t.length; a++) {
              if (-1 == e.indexOf(t[a])) {
                e.push(t[a])
              }
            }
            return e
          })((a = a.concat(t.data)))
          that.setData({
            order_list: a,
            storelist: a
          })
          console.log(a)
        }
      })
    },

    orderPay: function (t) {
      var e = getApp().globalData.getOpenId
      var a = uni.getStorageSync('users').id
      var o = t.currentTarget.dataset.id
      var s = t.currentTarget.dataset.money
      var n = t.currentTarget.dataset.type
      console.log(e, a, o, s, n)
      if ('5' == n) {
        uni.showModal({
          title: '提示',
          content: '您的支付方式为餐后支付，请到收银台付款'
        })
      } else {
        uni.showLoading({
          title: '正在提交',
          mask: true
        })
        app.globalData.util.request({
          url: 'entry/wxapp/QgPay',
          cachetime: '0',
          data: {
            openid: e,
            money: s,
            order_id: o
          },
          success: function (t) {
            console.log(t)
            uni.hideLoading()
            uni.requestPayment({
              timeStamp: t.data.timeStamp,
              nonceStr: t.data.nonceStr,
              package: t.data.package,
              signType: t.data.signType,
              paySign: t.data.paySign,
              success: function (t) {
                console.log(t.data)
              },
              complete: function (t) {
                console.log(t)
                if ('requestPayment:fail cancel' == t.errMsg) {
                  uni.showToast({
                    title: '取消支付',
                    icon: 'loading',
                    duration: 1000
                  })
                }
                if ('requestPayment:ok' == t.errMsg) {
                  uni.showToast({
                    title: '支付成功',
                    duration: 1000
                  })
                  if (1 == n) {
                    setTimeout(function () {
                      uni.redirectTo({
                        url: 'order?status=2'
                      })
                    }, 1000)
                  }
                  if (2 == n) {
                    setTimeout(function () {
                      uni.redirectTo({
                        url: 'order?status=4'
                      })
                    }, 1000)
                  }
                }
              }
            })
          }
        })
      }
    },

    canceldd: function (t) {
      var e = t.currentTarget.dataset.id
      console.log(e)
      uni.showModal({
        title: '提示',
        content: '是否取消该订单？',
        cancelText: '否',
        confirmText: '是',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/CancelOrder',
              cachetime: '0',
              data: {
                order_id: e
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '取消成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=5'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    orderRevoke: function (t) {
      var e = t.currentTarget.dataset.id
      console.log(e)
      uni.showModal({
        title: '提示',
        content: '是否删除该订单？',
        cancelText: '否',
        confirmText: '是',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/DelQgOrder',
              cachetime: '0',
              data: {
                order_id: e
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=3'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    txsj: function (t) {
      console.log('提醒商家' + t.currentTarget.dataset.tel)
      uni.makePhoneCall({
        phoneNumber: t.currentTarget.dataset.tel
      })
    },
    tzsjxq: function (id) {
      getApp().globalData.sjid = id
      uni.navigateTo({
        url: '/ymq_canyino2o/pages/seller/index'
      })
    },

    sqtk: function (e) {
      console.log('申请退款' + e.currentTarget.dataset.id)
      uni.showModal({
        title: '提示',
        content: '申请退款么',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/TkOrder',
              cachetime: '0',
              data: {
                order_id: e.currentTarget.dataset.id
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '申请成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=5'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    qrsh: function (t) {
      var e = t.currentTarget.dataset.id
      console.log(e)
      uni.showModal({
        title: '提示',
        content: '是否确认已收到货？',
        cancelText: '否',
        confirmText: '是',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/OkOrder',
              cachetime: '0',
              data: {
                order_id: e
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '收货成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=4'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    orderQrcode: function (a) {
      var that = this
      var s = that.order_list
      var n = a.target.dataset.index
      uni.showLoading({
        title: '正在加载',
        mask: true
      })
      if (that.order_list[n].offline_qrcode) {
        that.setData({
          hide: 0,
          qrcode: that.order_list[n].offline_qrcode
        })
        uni.hideLoading()
      } else {
        e.request({
          url: t.order.get_qrcode,
          data: {
            order_no: s[n].order_no
          },
          success: function (t) {
            if (0 == t.code) {
              that.setData({
                hide: 0,
                qrcode: t.data.url
              })
            } else {
              uni.showModal({
                title: '提示',
                content: t.msg
              })
            }
          },
          complete: function () {
            uni.hideLoading()
          }
        })
      }
    },

    hideFun: function (t) {
      this.setData({
        hide: 1
      })
    },

    hxqh: function (t) {
      var that = this
      var a = t.currentTarget.dataset.id
      var o = t.currentTarget.dataset.sjid
      console.log(a, o)
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      app.globalData.util.request({
        url: 'entry/wxapp/QgOrderCode',
        cachetime: '0',
        data: {
          order_id: a
        },
        success: function (t) {
          console.log(t.data)
          that.setData({
            hx_code: t.data,
            hide: 2
          })
        }
      })
    }
  }
}
</script>
<style>
@import './order.css';
</style>
